package com.qf.hospital.system.controller;

import com.qf.hospital.system.entity.*;
import com.qf.hospital.system.service.DepartmentsService;
import com.qf.hospital.system.service.DoctorService;
import com.qf.hospital.system.service.RegisteredTypeService;
import com.qf.hospital.system.service.ReportService;
import com.qf.hospital.system.util.R;
import com.qf.hospital.system.vo.ReportVO;
import com.qf.hospital.system.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class ReportController {

    @Autowired
    private ReportService reportService;

    @Autowired
    private DepartmentsService departmentsService;

    @Autowired
    private RegisteredTypeService registeredTypeService;

    @Autowired
    private DoctorService doctorService;

    /**
     * 用户挂号首页信息
     * # path&method
     *     Request URL: http://localhost:8080/cao/index
     *     Request Method: GET
     * # parameter
     * 	 cc = Integer  (1 - 查询全部,2 - 当天挂号,3 - 预约挂号)
     * 	 name = String (没有指定用户姓名,手机等内容,每个字段都做筛选)
     * # return
     * SUCCESS:
     * [
     *     {"reportId": 33,"reportName": "师霄","phone": "12222222222","carid": "111111111111111111","registeredType": "专家号","datime": "2021-11-11","department": "混合科","doctorName": "老郑"},
     *     {"reportId": 33,"reportName": "师霄霄","phone": "12222222222","carid": "111111111111111111","registeredType": "专家号","datime": "2021-11-11","department": "混合科","doctorName": "老郑郑"},
     *     ……
     * ]
     */
    @GetMapping("/cao/index")
    public List<ReportVO> index(Integer cc,String name){
        //1. 逻辑删除
        reportService.deleteBeforeReportUser();
        //2. 查询信息 //3. 返回信息
        return reportService.findByCCAndCondition(cc,name);
    }


    /**
     * 查询全部科室
     * # path&method
     *     Request URL: http://localhost:8080/cao/seldep
     *     Request Method: POST
     * # parameter
     * 	无
     * # return
     * [
     *     {"departmentId": 5,"department": "内科"},
     *     ...
     * ]
     */
    @PostMapping("/cao/seldep")
    public List<Departments> selDep(){
        return departmentsService.findAll();
    }


    /**
     * 查询全部挂号类型
     * # path&method
     *     Request URL: http://localhost:8080/cao/selreg
     *     Request Method: POST
     * # parameter
     * 	无
     * # return
     * [
     *     {"id": 5,"type": "专家号"},
     *     ...
     * ]
     */
    @PostMapping("/cao/selreg")
    public List<RegisteredType> selReg(){
        return registeredTypeService.findAll();
    }


    /**
     * 查询挂号费用
     * # path & method
     *     Request URL: http://localhost:8080/cao/selRegMoney
     *     Request Method: POST
     * # parameter
     * 	registeredId: Integer （挂号类型）
     * # return
     * 	返回Double类型的数值
     * 	20，30
     */
    @PostMapping("/cao/selRegMoney")
    public Double selRegMoney(Integer registeredId){
        //1. 校验
        if(registeredId == null){
            log.info("【查询挂号费用】 registeredId为必传项，岂能为空！");
            return null;
        }
        //2. 查询
        Double price = registeredTypeService.findPriceByRegisteredId(registeredId);
        //3. 返回
        return price;
    }


    /**
     * 查询医生信息
     * # path & method
     *     Request URL: http://localhost:8080/cao/seldoctor
     *     Request Method: POST
     * # parameter
     * 	registeredId: Integer（挂号类型）
     * 	departmentId: Integer（科室id）
     * # return
     * [
     *     {"doctorId": 2,"doctorName": "索拉卡"},
     *     ...
     * ]
     */
    @PostMapping("/cao/seldoctor")
    public List<Doctor> seldoctor(Integer registeredId,Integer departmentId){
        //1. jiao
        if(registeredId == null || departmentId == null){
            log.info("【查询医生信息】 参数不合法！！ registeredId = {}，departmentId = {}",registeredId,departmentId);
            return null;
        }
        //2. cha
        List<Doctor> result = doctorService.findDoctorByCondition(registeredId, departmentId);
        //3. fan
        return result;
    }

    /**
     * 手机号&身份证号查重
     * # path&method
     *     Request URL: http://localhost:8080/cao/phone
     *     Request Method: POST
     * # parameters
     *     phone: 手机
     *     carid: 身份证号
     * # return
     * 	1： 代表手机号重复
     * 	2： 代表身份证号重复
     * 	3： 可以正常添加
     */
    @PostMapping("/cao/phone")
    public Integer phone(String phone,String carid){
        //1. jiao
        if(phone == null || carid == null){
            log.info("【手机号&身份证号查重】 参数不合法！ phone = {},carid = {}",phone,carid);
            return 0;
        }
        //2. cha
        if(reportService.findCountByPhone(phone)){
            return 1;
        }
        if(reportService.findCountByCarid(carid)){
            return 2;
        }
        //3. return
        return 3;
    }


    /**
     * 添加挂号人信息
     * # path & method
     *     Request URL: http://localhost:8080/cao/addReport
     *     Request Method: POST
     * # parameters
     * 	id:
     *     report: 哈哈哈
     *     sex: 男
     *     age: 88
     *     users:
     *     phone: 18888888888
     *     carid: 888888888888888888
     *     departmentId: 8
     *     registerTypeId: 6
     *     doctorId: 19
     *     price: 30
     * # return
     * SUCCESS:
     * {
     *     "code": 0,
     *     "msg": "添加成功"
     * }
     * FAIL:
     * {
     *     "code": 110,
     *     "msg": "添加挂号人信息失败!"
     * }
     */
    @PostMapping("/cao/addReport")
    public ResultVO addReport(Report report){
        //1. 封装数据
        Subject subject = SecurityUtils.getSubject();
        SysUser sysUser = (SysUser) subject.getPrincipal();
        report.setUsers(sysUser.getRealName());

        //2. 调用Service保存
        reportService.addReport(report);

        //3. 响应
        return R.ok();

    }


}
